Shader "Unlit/ffff"
{
    Properties
    {
        // -------------------------------------
        // Hidden properties - Generic
        [HideInInspector]_VfxMode("__vfxmode", Vector) = (1,1,0,0)
        [HideInInspector]_Blend("__blendmode", Float) = 0.0
        // ------------------------------------
        [HideInInspector]_Cull("__cull", Float) = 1.0
        [HideInInspector]_SrcBlend("__src", Float) = 1.0
        [HideInInspector]_DstBlend("__dst", Float) = 0.0
        [HideInInspector]_SrcBlendAlpha("__srcA", Float) = 1.0
        [HideInInspector]_DstBlendAlpha("__dstA", Float) = 0.0
        /*[Enum(Normal,4,Always Top,8)] */
        [HideInInspector]_ZTest ("__zt", Float) = 4.0            
        [HideInInspector]_ColorMask ("Color Mask", Float) = 15
        [HideInInspector]_ZWrite("__zw", Float) = 0.0
        [HideInInspector]_StencilComp ("Stencil Comparison", Float) = 8
        [HideInInspector]_Stencil ("Stencil ID", Float) = 0
        [HideInInspector]_StencilOp ("Stencil Operation", Float) = 0
        [HideInInspector]_StencilWriteMask ("Stencil Write Mask", Float) = 255
        [HideInInspector]_StencilReadMask ("Stencil Read Mask", Float) = 255
        [HideInInspector]_AlphaToMask("__alphaToMask", Float) = 0.0        
        // [ToggleUI][HideInInspector] _AlphaClip("__clip", Float) = 0.0
        
        // -------------------------------------
        [Header(Global)]
        _GlobalAlpha("Global Alpha", Range(0.0,1.0)) = 1
        
        [Header(MAIN)]
        [HDR]_MainColor("MainColor", Color) = (1,1,1,1)
        _MainTex("MainTex", 2D) = "white" {}
        _MainUValue("MainTex UValue", Float) = 0
        _MainVValue("MainTex VValue", Float) = 0
        _MainBrightness("MainTex Brightness", Float) = 1
        _MainAlpha("MainTex Transparent", Float) = 1
        _MainFilterValue4("MainTex FilterValue4", Vector) = (0,0,0,0)
        _MainMatrix0("MainTex Matrix", Vector) = (1,0,0,0)
        _MainMatrix1("MainTex Matrix", Vector) = (0,1,0,0)
        _MainSW("MainTex SW", Vector) = (0,0,0,0)
        
        
        [Header(DISTORT)]
        [Toggle] _FDistortEnabled("FDistort", Float) = 0
        _DistortTex("Distort", 2D) = "white" {} 
        _DistortFilterValue4("Distort FilterValue4", Vector) = (0,0,0,0)
        _DistortUValue("Distort UValue", Float) = 0
        _DistortVValue("Distort VValue", Float) = 0
        _DistortBrightness("Distort Brightness", Float) = 1   
        _DistortMatrix0("Distort Matrix", Vector) = (1,0,0,0)
        _DistortMatrix1("Distort Matrix", Vector) = (0,1,0,0)
        _DistortSW("Distort SW", Vector) = (0,0,0,0)
        
        
        [Header(DISSOLVE)]
        [Toggle] _FDissolveEnabled("FDissolveTex", Float) = 0
        _DissolveTex("Dissolve Tex", 2D) = "white" {}
        // _DissolveBrightness("Dissolve Brightness", Float) = 1
        _DissolveUValue("Dissolve UValue", Float) = 0
        _DissolveVValue("Dissolve VValue", Float) = 0
        // _DissolveBrightness("Dissolve Brightness", Float) = 1
        // _DissolveTransparent("Dissolve Transparent", Float) = 1
        _DissolveFilterValue4("Distort FilterValue4", Vector) = (0,0,0,0)
        _DissolveMatrix0("Dissolve Matrix", Vector) = (1,0,0,0)
        _DissolveMatrix1("Dissolve Matrix", Vector) = (0,1,0,0)
        _DissolveSW("Dissolve SW", Vector) = (0,0,0,0) 

        [HDR]_DissolveLineColor("Dissolve Line Color", Color) = (1,1,1,1)
        _DissolveDateValue("Dissolve Date", Vector) = (0,0,0,0) 
        _DissolveFactor("DissolveFactor", Range( 0.0 , 1.0)) = 0 
        
        
        [Header(FNL)]
        [Toggle] _FFnlEnabled("FFnl", Float) = 0
        [HDR]_FnlColor("FnlColor", Color) = (1,1,1,1)
        _FnlValue("FnlValue", Vector) = (0,0,0,0) 
        
        [Header(GROUND)]
        [Toggle] _FGroundEnabled("FGround", Float) = 0
        _GroundClipHight("Ground Clip Hight", Float) = 0
        _GroundClipSoft("Ground Clip Soft", Float) = 0.5
    }
    SubShader
    {
        Tags 
        {
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "RenderType"="Transparent"
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }
        // -------------------------------------
        // Render State Commands
        // BlendOp[_BlendOp]
        Blend[_SrcBlend][_DstBlend], [_SrcBlendAlpha][_DstBlendAlpha] 
        ZWrite[_ZWrite]
        ZTest[_ZTest]
        Cull[_Cull]
        AlphaToMask[_AlphaToMask]
        ColorMask  [_ColorMask]

        Stencil
        {
            Ref[_Stencil]
            Comp[_StencilComp]
            Pass[_StencilOp]
            ReadMask[_StencilReadMask]
            WriteMask[_StencilWriteMask]
        }



        HLSLINCLUDE

            #pragma multi_compile_local _ UNITY_UI_CLIP_RECT
            #include "DTURPVFXUtil.hlsl"

            DT_SAMPLER2D(_MainTex);
            DT_SAMPLER2D(_ColorTex);
            DT_SAMPLER2D(_DistortTex);
            DT_SAMPLER2D(_DissolveTex);
            DT_SAMPLER2D(_MaskTex);

            CBUFFER_START(UnityPerMaterial)
                DTfloat _GlobalAlpha;

                DTfloat4 _MainTex_ST;
                DTfloat4 _MainFilterValue4;        
                DTfloat4 _MainMatrix0;
                DTfloat4 _MainMatrix1;
                DTfloat4 _MainSW;

                DTfloat4 _DistortTex_ST;
                DTfloat4 _DistortFilterValue4;        
                DTfloat4 _DistortMatrix0;
                DTfloat4 _DistortMatrix1;
                DTfloat4 _DistortSW;
                DTfloat _FDistortEnabled;

                DTfloat4 _DissolveTex_ST;
                DTfloat4 _DissolveFilterValue4;        
                DTfloat4 _DissolveMatrix0;
                DTfloat4 _DissolveMatrix1;
                DTfloat4 _DissolveSW; 
                DTfloat4 _DissolveDateValue;
                DTfloat _FDissolveEnabled;   

                
                DTfloat _FFnlEnabled;  
                DTfloat4 _FnlColor;
                DTfloat4 _FnlValue; 

                DTfloat _FGroundEnabled;
                DTfloat _GroundClipSoft,_GroundClipHight;

                DTfloat _CBLocalThreshold;
                DTfloat4 _CBLocalColor;
                DTfloat _CBLocalLight;
                DTfloat _CBGlobalBool;
                DTfloat _CBLocalBanBool;
                
                DTfloat4 _ClipRect;
                DTfloat _UIMaskSoftnessX;
                DTfloat _UIMaskSoftnessY;
            CBUFFER_END

            //------------------------------
            //Anim Properties            
            DTfloat4 _MainColor;
            DTfloat _MainUValue;
            DTfloat _MainVValue;            
            DTfloat _MainRotValue;
            DTfloat _MainBrightness;
            DTfloat _MainAlpha;

            DTfloat _DistortUValue;
            DTfloat _DistortVValue;
            DTfloat _DistortBrightness;

            DTfloat _DissolveUValue;
            DTfloat _DissolveVValue;
            // DTfloat _DissolveBrightness;
            DTfloat4 _DissolveLineColor; 
            DTfloat _DissolveFactor;
        ENDHLSL 

        Pass
        {
            Name "VFXForward"
            Tags
            {
                "RenderType"="Transparent" 
                "LightMode" = "UniversalForward"
            }
            // Blend [_Scr] [_Dst], One OneMinusSrcAlpha
            // ZWrite Off
            // ZTest  [_ZTest]
            // Offset 0 , 0

            HLSLPROGRAM
            #pragma vertex DT_VFXvert
            #pragma fragment frag

           

            FragTexUVDate SetUVDate_BIT(v2f_DT_VFX i ,DTfloat2 uvval, DTfloat4 uvfilter, 
                DTfloat4 m0, DTfloat3 m1, float4 switchBit)
            {
                // DTfloat dt_GetBITFloat(int value,int index,int len)
                FragTexUVDate d = (FragTexUVDate)0;
                
                int sw1 = (int)switchBit.y;
                //-xxx #### xxxx xxxx
                d.IsSceneUV = dt_GetBITFloat(sw1,0,1);
                d.IsPolar = dt_GetBITFloat(sw1,1,1);
                d.IsClampX = dt_GetBITFloat(sw1,2,1);
                d.IsClampY = dt_GetBITFloat(sw1,3,1);
                d.IsFilpX = dt_GetBITFloat(sw1,4,1);
                d.IsFilpY = dt_GetBITFloat(sw1,5,1);
                //-xx# xxxx xxxx xxxx
                d.IsDistort = dt_GetBITFloat(sw1,13,1);
                d.IsFrame = dt_GetBITFloat(sw1,14,1);

                const float UOffsets[] = {
                    uvval.x,dt_AutoValue(uvfilter.x),
                    i.Custom1.x,i.Custom1.y,i.Custom1.z,i.Custom1.w,
                    i.Custom2.x,i.Custom2.y,i.Custom2.z,i.Custom2.w,
                };
                const float VOffsets[] = {
                    uvval.y,dt_AutoValue(uvfilter.y),
                    i.Custom1.x,i.Custom1.y,i.Custom1.z,i.Custom1.w,
                    i.Custom2.x,i.Custom2.y,i.Custom2.z,i.Custom2.w,
                };
                d.UVDistValue = uvfilter.zw;

                int sw0 = (int)switchBit.x;
                //-xxx xxxx xxxx ####
                int Umode = clamp(dt_GetBITFloat(sw0,0,4),0,15);                
                //-xxx xxxx #### xxxx
                int Vmode = clamp(dt_GetBITFloat(sw0,4,4),0,15);
                //Float
                d.Uvalue = UOffsets[Umode % 10];
                d.Vvalue= VOffsets[Vmode % 10];
                d.RotValue = m0.w;
                //Float3
                d.MatrM0 = m0.xyz;
                d.MatrM1 = m1;
                return d;
            }


            DTfloat4 frag (v2f_DT_VFX i) : SV_Target
            {
                UNITY_SETUP_INSTANCE_ID( i );
                UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX( i );

                //Dist
                FragTexUVDate DistUVDate = (FragTexUVDate)0;
                DistUVDate = SetUVDate_BIT(i,DTfloat2(_DistortUValue,_DistortVValue),
                            _DistortFilterValue4,_DistortMatrix0,_DistortMatrix1,_DistortSW);                
                float2 distUV = GetFragTexUV(i,DistUVDate,0);                
                DTfloat distCol = DT_TEX2D(_DistortTex, distUV).r;
                distCol *= _DistortBrightness;
                distCol *= _FDistortEnabled;
 
                //Dissolve
                FragTexUVDate DissUVDate = (FragTexUVDate)0;
                DissUVDate = SetUVDate_BIT(i,DTfloat2(_DissolveUValue,_DissolveVValue),
                            _DissolveFilterValue4,_DissolveMatrix0,_DissolveMatrix1,_DissolveSW);                
                float2 dissUV = GetFragTexUV(i,DissUVDate,distCol);                
                DTfloat dissCol = DT_TEX2D(_DissolveTex, dissUV).r;

                //DissolveData                 
                DissolveTexData dissData = Set_DissolveData(i, dissCol,
                    _FDissolveEnabled,distCol,_DissolveLineColor,
                    _DissolveDateValue,_DissolveFactor);
                float2 dissv = Get_DissolveClip(dissData);
                 

                //Ground
                DTfloat groundValue = 1;
                if(_FGroundEnabled > 0)
                { 
                    float groundHight = i.positionWS.y;
                    groundValue = smoothstep(_GroundClipHight, _GroundClipHight + _GroundClipSoft,groundHight);
                }

                //Fnl 
                FNLData fnlData = (FNLData)0;
                fnlData.IsOn = _FFnlEnabled > 0;
                fnlData.FNL_Color = _FnlColor;
                fnlData.Scale =  _FnlValue.x;
                fnlData.Power =  _FnlValue.y;                
                fnlData.Brightness =  _FnlValue.z;
                fnlData.SW = _FnlValue.w;                
                DTfloat fnlValue = Get_FNLValue(i,fnlData,distCol);

                
                    

                //main
                FragTexUVDate mainUVDate = (FragTexUVDate)0;
                mainUVDate = SetUVDate_BIT(i,DTfloat2(_MainUValue,_MainVValue),
                            _MainFilterValue4,_MainMatrix0,_MainMatrix1,_MainSW);                
                float2 mainUV = GetFragTexUV(i,mainUVDate,distCol);                
                DTfloat4 mainCol = DT_TEX2D(_MainTex, mainUV) * _MainColor;
                mainCol.rgb *= _MainBrightness;
                mainCol.rgb = lerp(mainCol.rgb,dissData.LineColor, dissv.y  );
                mainCol.a *= _MainAlpha;



                //finalColor
                DTfloat3 finalColor = DTfloat3(0,0,0);
                finalColor += mainCol.rgb;
                finalColor += Get_FNLCol(finalColor, fnlValue, fnlData); 
                //finalAlpha
                DTfloat finalAlpha = 0;
                mainCol.a = mainCol.a * dissv.x ;
                finalAlpha = saturate(mainCol.a); 
                finalAlpha *= Get_FNLAlp(fnlValue, fnlData);
                finalAlpha *= groundValue;
                
                finalAlpha = saturate(finalAlpha * _GlobalAlpha); 

                return DTfloat4(finalColor,finalAlpha);

            }
            ENDHLSL
        }
    }
    CustomEditor "DT.Art.VFX.DTEditorGUI.DTFullModuleGUI"
    Fallback "Hidden/InternalErrorShader"
    // 
}
